In this work it will be analyzed the impact in quality of several parameters describing red wine. The dataset is curated by Udacity and comes from UCI repository https://archive.ics.uci.edu/ml/datasets/Wine+Quality and consists of 1599 sample data for Red wine https://docs.google.com/document/d/1qEcwltBMlRYZT-l699-71TzInWfk4W9q5rTCSvDVMpc/pub?embedded=true.
In Cortez et al. (2009) it is shown that the most imporant features for assessing Red Wine quality are:
sulphates
pH
total sulfur dioxide
# Load the Data
redwines <- read.csv('wineQualityReds.csv')
dim(redwines)
## [1] 1599 13
#names(redwines)
summary(redwines)
## X fixed.acidity volatile.acidity citric.acid
## Min. : 1.0 Min. : 4.60 Min. :0.1200 Min. :0.000
## 1st Qu.: 400.5 1st Qu.: 7.10 1st Qu.:0.3900 1st Qu.:0.090
## Median : 800.0 Median : 7.90 Median :0.5200 Median :0.260
## Mean : 800.0 Mean : 8.32 Mean :0.5278 Mean :0.271
## 3rd Qu.:1199.5 3rd Qu.: 9.20 3rd Qu.:0.6400 3rd Qu.:0.420
## Max. :1599.0 Max. :15.90 Max. :1.5800 Max. :1.000
## residual.sugar chlorides free.sulfur.dioxide
## Min. : 0.900 Min. :0.01200 Min. : 1.00
## 1st Qu.: 1.900 1st Qu.:0.07000 1st Qu.: 7.00
## Median : 2.200 Median :0.07900 Median :14.00
## Mean : 2.539 Mean :0.08747 Mean :15.87
## 3rd Qu.: 2.600 3rd Qu.:0.09000 3rd Qu.:21.00
## Max. :15.500 Max. :0.61100 Max. :72.00
## total.sulfur.dioxide density pH sulphates
## Min. : 6.00 Min. :0.9901 Min. :2.740 Min. :0.3300
## 1st Qu.: 22.00 1st Qu.:0.9956 1st Qu.:3.210 1st Qu.:0.5500
## Median : 38.00 Median :0.9968 Median :3.310 Median :0.6200
## Mean : 46.47 Mean :0.9967 Mean :3.311 Mean :0.6581
## 3rd Qu.: 62.00 3rd Qu.:0.9978 3rd Qu.:3.400 3rd Qu.:0.7300
## Max. :289.00 Max. :1.0037 Max. :4.010 Max. :2.0000
## alcohol quality
## Min. : 8.40 Min. :3.000
## 1st Qu.: 9.50 1st Qu.:5.000
## Median :10.20 Median :6.000
## Mean :10.42 Mean :5.636
## 3rd Qu.:11.10 3rd Qu.:6.000
## Max. :14.90 Max. :8.000
# New variables
#redwines$quality.factor <- factor(redwines$quality)
redwines$quality.cat <- NA
redwines$quality.cat <- ifelse(redwines$quality>=7, 'good','medium')
redwines$quality.cat <- ifelse(redwines$quality<=4, 'bad',redwines$quality.cat) # if not, leave the previous values
redwines$quality.cat <- factor(redwines$quality.cat, levels = list('bad', 'medium','good')) # set the order!
print("Variables after dividing into quality groups")
## [1] "Variables after dividing into quality groups"
str(redwines) #summary of values for each variable
## 'data.frame': 1599 obs. of 14 variables:
## $ X : int 1 2 3 4 5 6 7 8 9 10 ...
## $ fixed.acidity : num 7.4 7.8 7.8 11.2 7.4 7.4 7.9 7.3 7.8 7.5 ...
## $ volatile.acidity : num 0.7 0.88 0.76 0.28 0.7 0.66 0.6 0.65 0.58 0.5 ...
## $ citric.acid : num 0 0 0.04 0.56 0 0 0.06 0 0.02 0.36 ...
## $ residual.sugar : num 1.9 2.6 2.3 1.9 1.9 1.8 1.6 1.2 2 6.1 ...
## $ chlorides : num 0.076 0.098 0.092 0.075 0.076 0.075 0.069 0.065 0.073 0.071 ...
## $ free.sulfur.dioxide : num 11 25 15 17 11 13 15 15 9 17 ...
## $ total.sulfur.dioxide: num 34 67 54 60 34 40 59 21 18 102 ...
## $ density : num 0.998 0.997 0.997 0.998 0.998 ...
## $ pH : num 3.51 3.2 3.26 3.16 3.51 3.51 3.3 3.39 3.36 3.35 ...
## $ sulphates : num 0.56 0.68 0.65 0.58 0.56 0.56 0.46 0.47 0.57 0.8 ...
## $ alcohol : num 9.4 9.8 9.8 9.8 9.4 9.4 9.4 10 9.5 10.5 ...
## $ quality : int 5 5 5 6 5 5 5 7 7 5 ...
## $ quality.cat : Factor w/ 3 levels "bad","medium",..: 2 2 2 2 2 2 2 3 3 2 ...
#unique(redwines$quality.cat)
In this section it will be analyzed each of the variables describing the wines.
The distribution of wine shows that most of wines have a quality between 5-6 points.
Figure 3.1: Quality distribution
In the next plot it is deduced that the quality of the wine is directly proportional to the fixed acidity and acid levels and inversely proportional to volatile acidity.
Figure 3.2: Fixed and volatile acidity distributions
The plot of residual suggar shows that the better the wine the higher the residual sugar levels.
Figure 3.3: Residual sugar distribution
In the plot of the chlorides, it can be onserved that the better the wine, the lower the chloride levels. For the sulfur dioxide, either the free or the total sulfur dioxide, high levels are indicator of medium quality, whereas bad and good wine have the same low amount of sulfure dioxide.
Figure 3.4: Chlorides and sulfur dioxide distributions
The next plot shows that, generally, the lower the density, the better the quality of the wine. ALso, low pH levels are sign of beter quality. The higher the sulphates level, the better quality of the wine and also, good wines have higher amount of alcohol.
Figure 3.5: Density, pH, sulphates and alcohol distributions
There are 1599 red wines with 12 features (fixed.acidity, volatile.acidity, citric.acid, residual.sugar, chlorides, free.sulfur.dioxide, total.sulfur.dioxide, density, pH, sulphates, alcohol and quality). The variables quality is converted in a factor variable (adding a new variable named quality.cat) with the following levels:
| BAD | MEDIUM | GOOD | |
|---|---|---|---|
quality.cat |
[0,4] | (4,7) | [7,10] |
Other observations:
| Min. | 1st Qu. | Median | Mean | 3rd Qu. | Max. | |
|---|---|---|---|---|---|---|
redwines.quality |
3 | 5 | 6 | 5.636 | 6 | 8 |
Table 3.1 shows that the mean quality of the red wines is 5.636 and the median is 6. Q1 division corresponds to 5 and Q3 to 6, hence, 50% of the data lays within 5-6 range of quality, this is the level MEDIUM.
The main feature of interest in this dataset is the quality of the wine.
In Cortez et al. (2009) it is shown that the most imporant features for assessing Red Wine quality are: sulphates, pH and total sulfur dioxide. An analysis will be performed in order to certify whether these are the most importart features for the analysis or others are.
I did not create any new variables to support the analysis since the amount of information available is enough to assess the quality of the wine.
Most of the features had a normal distribution. Some of the them had quite skewed distributions and many oultiers and I performed a log10 tranformation in order to have a better view. The efect of this transformation can be observed in figure 3.6, where charateristics of plots in the left side are seen better after the x axis is transformed.
Figure 3.6: Residual sugar and Chlorides distributions with original and log10() transformed axis
In this section it will be perfomed an analysis of the features vs the quality ranges.
In the next plot (fig.4.1) it is deduced that the quality of the wine is directly proportional to the fixed acidity and acid levels and inversely proportional to volatile acidity.
Figure 4.1: Fixed and volatile acidity distributions per quality segment
The plot of residual suggar (4.2) shows that the better the wine the higher the residual sugar levels.
Figure 4.2: Residual sugar distribution per quality segment
In the plot of the chlorides, it can be onserved that the better the wine, the lower the chloride levels. For the sulfur dioxide, either the free or the total sulfur dioxide, high levels are indicator of medium quality, whereas bad and good wine have the same low amount of sulfure dioxide.
Figure 4.3: Chlorides and sulfur dioxide distributions per quality segment
The next plot shows that, generally, the lower the density, the better the quality of the wine. Also, low pH levels are sign of beter quality. The higher the sulphates level, the better quality of the wine and also, good wines have higher amount of alcohol.
Figure 4.4: Density, pH, suplhates and alcohol distributions per quality segment
A good way to see how to variables are related is using the correlation. In this sectio it will be calculated the correlation between the features of red wine and the strongest will be analyzed.
In the next figure (fig.4.5) it is shown the correlation factor between the different variables in the Red Wines dataset. Significant correlation values (\(|\rho|>0.3\)) are highlighted with stronger color density the higher the correlation value.
For the purpose of this analysis, the focus will be placed in the relationship between quality and other variables.
Figure 4.5: Correlation bewteen Red Wine’s dataset features
Figure 4.6: Correlation vs Quality
From the general correlation matrix (fig.4.5), three main variables can be selected due to their high correlation with quality: alcohol (\(\rho=0.5\)), volatile.acidity (\(\rho=0.4\)) and sulphates (\(\rho=0.3\)). In order to see if they are suitable to perform an analysis let us explore the correlation between them and also the distribution of samples in bivariate plots in figure 4.7.
Figure 4.7: Correlation between alcohol, volatile acidity and sulphates
From plot 4.7 it is observed that there is a strong relationship between the sulphates and volatile acidity, and less strong between alcohol and volatile acidity.
Let us now explore the three variables suggested in Cortez et al. (2009) as the most imporant features for assessing Red Wine quality are: sulphates, pH and total sulfur dioxide.
Figure 4.8: Correlation between total sulfur dioxide, pH and sulphates
Figure 4.7 shows that the strongest relationship is between sulphates and pH (\(\rho=-0.2\)) followed by pH-total sulfur dioxide.
From figure 4.8 and 4.7 it can be concluded the varibles in the second figure (sulphates, pH and total sulfur dioxide), the ones suggested by Cortez et al. (2009) are less correlated between them and lead to a more accurate analysis.
For the three main variables (fig.4.9) influencing the quality of wine:
Figure 4.9: Sulphates, pH and total sulfur dioxide vs quality
Figure 4.5 shows the correlation between the variables in the dataset. The strongest are these pairs: citric acid-fixed acidity, total sulfur dioxide - free sulfur dioxide, density- fixed acidity and pH - fixed acidity.
It is logical that citric acid influences fixed acidity, as does free sulfur dioxide with total sulfur dioxide. Ph is known to be related to the acid levels, so no surprise here. I did not know there was such a strong relationship between density and fixed acidity.
Figure 4.10 shows the relationship between fixed acidity and density, and between fixed acidity and pH. Fixed acidity and density have a strong positive correlation (\(\rho=0.7\)) and fixed acidity and pH show a strong negative correlation (\(\rho=-0.7\)).Figure 4.10: Density and pH vs fixed acidity
Cortez, Paulo, António Cerdeira, Fernando Almeida, Telmo Matos, and José Reis. 2009. “Modeling Wine Preferences by Data Mining from Physicochemical Properties.” Decision Support Systems 47 (4). Elsevier: 547–53.